<template>
  <div class="app-container">
    <el-form ref="dataForm" :rules="qualityFormRules" :model="qualityForm" label-position="left" label-width="140px">
      <el-form-item label="规则名称" prop="title">
        <el-input v-model="qualityForm.title" />
      </el-form-item>
      <el-form-item label="扫描时段" prop="fullname">
        <el-input v-model="qualityForm.fullname" />
      </el-form-item>
      <el-form-item label="生效成员" prop="siteUrl">
        <el-input v-model="qualityForm.siteUrl" />
      </el-form-item>
      <el-form-item label="选择分组" prop="industrialID">
          <el-select v-model="qualityForm.sectorsIndustrialID" placeholder="请选择">
            <el-option
              v-for="item in industrialOptions"
              :key="item.ID"
              :label="item.title"
              :value="item.ID"
            />
          </el-select>
      </el-form-item>
      <el-form-item label="设置关键词" prop="address">
        <el-input v-model="qualityForm.address" aria-placeholder="模糊匹配" />
        <el-input v-model="qualityForm.address" aria-placeholder="精确匹配" />
      </el-form-item>
      <el-form-item label="自动打标签" prop="remark">
        每
        天
        触发关键词
        <el-input v-model="qualityForm.address" aria-placeholder="模糊匹配" />
        次自动打上
        <el-input v-model="qualityForm.address" aria-placeholder="模糊匹配" />
        标签


      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCannel()">
        取消
      </el-button>
      <el-button type="primary" @click="createData()">
        保存并发布
      </el-button>
      <el-button v-if="qualityForm.status.value <= 5" type="primary" @click="draftData()">
        保存
      </el-button>
      <el-button v-if="qualityForm.status.value === 5 && qualityForm.ID > 0" type="danger" @click="handleDelete(qualityForm)">
        删除
      </el-button>
    </div>
  </div>
</template>

<script>
import { saveQuality, removeQualityByID, getQualityDetail } from '@/api/knowledge/quality'
import { getIndustrialUsable } from '@/api/knowledge/industrial'
import Upload from '@/components/Upload/SingleImage2' // secondary package based on el-pagination
import waves from '@/directive/waves' // waves directive
import { } from '@/utils'

export default {
  name: 'ComplexTable',
  components: { Upload},
  directives: { waves },
  filters: {
  },
  data() {
    return {
      tableKey: 0,
      qualityForm: {
        code: '',
        title: '',
        qualityID:0,
        employeeID:0,
        remark: '',
        status: {
          label: '',
          value: 1
        }
      },
      industrialOptions:[],                      //部门
      industriesIndustrialOptions:[],            //产业
      superSectorsIndustrialOptions:[],          //行业
      basicSectorIndustrialOptions:[],           //基本行业
      subsectorsSectorIndustrialOptions:[],      //子行业

      stateDistrictOptions:[],          //州
      regionDistrictOptions:[],         //区
      countryDistrictOptions:[],        //国家
      provinceDistrictOptions:[],       //省
      cityDistrictOptions:[],          ///市
      countyDistrictOptions:[],         //县
      townDistrictOptions:[],           //县
      qualityFormRules: []
    }
  },
  created(options) {
    this.loadEmployeeList()
    let parentQualityID = this.$route.query && this.$route.query.parentQualityID
    if(parentQualityID != undefined){
      this.qualityForm.qualityID = parentQualityID
    }
    let qualityID = this.$route.query && this.$route.query.qualityID
    if (qualityID !== undefined) {
      qualityID = parseInt(qualityID)
      if (!isNaN(qualityID)) {
        this.listLoading = true
        const formData = {
          'mechanismID': qualityID
        }
        getQualityDetail(formData).then(response => {
          if (response.status === 200) {
            this.qualityForm = response.data
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }
    }
    console.log(qualityID)
  },
  methods: {
    loadEmployeeList() {
      getIndustrialUsable().then(response => {
        if (response.status === 200) {
          this.industrialOptions = response.data.list
        }
      })
    },
    handleCannel() {
      this.$router.push({ path: 'quality' })
    },
    draftData() {
      this.listLoading = true
      const _qualityForm = this.qualityForm
      _qualityForm.status = 1
      if (_qualityForm.ID > 0) {
        _qualityForm.mechanismID = _qualityForm.ID
      }
      saveQuality(_qualityForm).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'quality' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    createData() {
      this.listLoading = true
      const _qualityForm = this.qualityForm
      _qualityForm.status = 6
      if (_qualityForm.ID > 0) {
        _qualityForm.mechanismID = _qualityForm.ID
      }
      saveQuality(_qualityForm).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'quality' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    handleDelete(row, index) {
      const fromdata = {
        'qualityID': row.ID
      }
      removeQualityByID(fromdata).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'quality' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
